package com.xyang.demo.controller;

import com.github.pagehelper.Page;
import com.xyang.demo.entity.Bxb;
import com.xyang.demo.entity.PageResult;
import com.xyang.demo.service.BxbServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Stars-Of-Future-xYang
 * @version 1.0
 * @date 2021/3/19 11:03
 */

@RestController
@RequestMapping("/bxb")
public class BxbController {

    @Autowired
    private BxbServiceImpl service;

    @RequestMapping("/findAll")
    public PageResult findAll(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResult pageResult = service.findAll(currentPage, pageSize);
        return pageResult;
    }

    @RequestMapping("/add")
    public Map<String, Object> add(@RequestBody Bxb bxb) {
        HashMap<String, Object> map = new HashMap<>();
        service.add(bxb);
        map.put("msg", "success");
        map.put("flag", true);
        return map;
    }

    @RequestMapping("/update")
    public Map<String, Object> upadte(@RequestBody Bxb bxb, Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        bxb.setId(id);
        service.update(bxb);
        map.put("msg", "success");
        map.put("flag", true);
        return map;
    }

    @RequestMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable("id")  Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        service.delete(id);
        map.put("msg", "success");
        map.put("flag", true);
        return map;
    }

    @RequestMapping("/findAllTotalPrice")
    public Map<String, Object> findAll() {
        HashMap<String, Object> map = new HashMap<>();
        Double total=service.findAllTotalPrice();
        map.put("msg", "success");
        map.put("flag", true);
        map.put("data",total);
        return map;
    }

}
